<?php
/*
 * Controlador de inicio de sesión de usuarios
 *
 * Autores: 
 *                - Fuck.inc
 */
 
class ProfileController extends AppController
{
	public function index()
	{
	}
	
	// Actualiza la imagen de avatar del usuario
	public function image()
	{
		if (Input::hasPost("postfile"))
		{
			if (Auth::is_valid())
			{
				$img = Upload::factory('fileimage', 'image');
				$img->setTypes(array('jpeg', 'gif', 'png'));
				$img->setMaxWidth(200);
				$img->setMaxHeight(200);
				
				$prefix = substr(md5(uniqid(rand())),0,6);
				$imgName = $prefix . "_" . Auth::get('account');
				$imgName .= "." . $img->getExtension();
				
				if ($img->save($imgName))
				{
					if ($this->users->find_first("conditions: account='" . Auth::get('account') . "'"))
					{
						$this->users->image = $imgName;
						if (!$this->users->save())
						{
							$this->respuesta = array("error" => true, "message" => ucfirst(_("there was an error saving the data")) . ". " . ucfirst(_("please, try again later")) . ".", "func" => "saySimple");
							View::select("error");
						}
						else
							Auth::set('image', $imgName); // Cambiamos el valor de la variable "image" de sesión de la identidad actual
					}
					else
					{
						$this->respuesta = array("error" => true, "message" => "fuck1", "func" => "saySimple");
						View::select("error");
					}
				}
				else
				{
					$msgError = $this->messageError($img->getError());
					$this->respuesta = array("error" => true, "message" => $msgError, "func" => "tellSimple");
					View::select("error");
				}
			}
			else
			{
				$this->respuesta = array("error" => true, "message" => ucfirst(_("it seems that you can't do this. Please, try again and if you can't, contact us to resolve the problem")) . ".", "func" => "tellSimple");
				View::select("error");
			}
		}
		
		View::response('view');
	}
	
	// Función que obtiene el mensaje de error de subida de una imagen
	protected function messageError($numError)
	{
		switch($numError)
		{
			case UPLOAD_ERR_INI_SIZE:		$message = ucfirst(_("the maximum file size permitted for uploading is 2 MB"));
										break;
										
			case UPLOAD_ERR_PARTIAL:		
			case UPLOAD_ERR_NO_FILE:		$message = ucfirst(_("there was an error uploading the file. Please, try again"));
										break;
										
			case 15:						$message = ucfirst(_("the file must be an image"));
										break;
										
			case 17: 						$message = ucfirst(_("the image width can't be much wider than 200px"));
										break;
										
			case 19:						$message = ucfirst(_("the image height can't be much wider than 200px"));
										break;
			
			case 20:						$message = ucfirst(_("this image type isn't supported by our system"));
										break;
										
			case UPLOAD_ERR_FORM_SIZE:
			case UPLOAD_ERR_NO_TMP_DIR:
			case UPLOAD_ERR_CANT_WRITE:
			case UPLOAD_ERR_EXTENSION:	
			default:						$message = ucfirst(_("there was an unexpected error uploading the file. Please, try again. If the error appears again, contact us to resolve the problem"));
										break;
		}
		
		return $message . ".";
	}
}

?>